package com.taxi.driver.module.main.mine.evaluation;

import com.qianxx.network.RequestError;
import com.qianxx.utils.RxUtil;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.module.vo.CommentVO;
import com.taxi.driver.module.vo.EvaluationVO;

import javax.inject.Inject;

public class EvaluationPresenter extends BasePresenter implements EvaluationContract.Presenter {

    EvaluationContract.View mView;
    UserRepository mUserRepository;

    @Inject
    public EvaluationPresenter(UserRepository userRepository,EvaluationContract.View view) {
        this.mUserRepository=userRepository;
        mView = view;
    }

    @Override
    public void reqEvaluation() {
        mUserRepository.getEvaluates()
                .map(EvaluationVO::createFrom)
                .compose(RxUtil.applySchedulers())
                .subscribe(vo -> {
                    mView.showEvaluation(vo);
                }, ex -> {
                    showNetworkError(ex, R.string.network_error,mView,mUserRepository);
                    mView.reqFail(0, "");
                    Throwable throwable = ex.getCause();
                    if (throwable instanceof RequestError) {
                        RequestError error = ((RequestError) throwable);
                        mView.reqFail(error.getErrCode(), error.getMessage());
                    }
                });

    }

    @Override
    public void reqYesterdayEvaluates(int nowPage) {
        mUserRepository.getYesterdayEvaluates(nowPage)
                .flatMapIterable(entities -> entities)
                .map(CommentVO::createFrom)
                .toList()
                .compose(RxUtil.applySchedulers())
                .subscribe(vos -> {
                    mView.showYesterdayEvaluates(vos);
                }, ex -> {
                    mView.reqFail(0, "");
                    Throwable throwable = ex.getCause();
                    if (throwable instanceof RequestError) {
                        RequestError error = ((RequestError) throwable);
                        mView.reqFail(error.getErrCode(), error.getMessage());
                    }
                });
    }

    @Override
    public int driverType() {
        return mUserRepository.getDriverType();
    }
}
